package com.apollow.oschina.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apollow.oschina.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CustomPopDialog extends Dialog {

    @BindView(R.id.tv_content)
    TextView mTvContent;
    @BindView(R.id.rootLayout)
    LinearLayout mRootLayout;
    @BindView(R.id.iv_code)
    ImageView mIvCode;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    private String mMessageStr;//从外界设置的消息文本
    private Bitmap mBitmap;
    private Drawable mDrawable;

    public CustomPopDialog(Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share_qrcode);
        ButterKnife.bind(this);
        //按空白处取消动画
        setCanceledOnTouchOutside(true);
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    private void initData() {
        if (mMessageStr != null) {
            mTvContent.setText(mMessageStr);
        }
        if (mBitmap != null) {
            mIvCode.setImageBitmap(mBitmap);
        }
        if (mDrawable != null) {
            mTvContent.setCompoundDrawables(null, mDrawable, null, null);
        }
    }

    //从外界设置二维码
    public void setBitmap(Bitmap bitmap) {
        this.mBitmap = bitmap;
    }

    public void setDrawable(Drawable drawable) {
        this.mDrawable = drawable;
    }

    private void initEvent() {
        mIvCode.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnLongClickListener != null) {
                    mOnLongClickListener.onLongClick();
                }
                return false;
            }
        });
    }

    public interface onLongClickListener {
        public void onLongClick();
    }

    private onLongClickListener mOnLongClickListener;

    public void setOnLongClickListener(onLongClickListener onLongClickListener) {
        this.mOnLongClickListener = onLongClickListener;
    }
}